package com.chahu.service.impl;

import com.chahu.entity.Order;
import com.chahu.entity.Snack;
import com.chahu.mapper.SearchMapper;
import com.chahu.service.SearchService;
import com.chahu.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Collections;
import java.util.List;

public class SearchServiceImpl implements SearchService {

    @Override
    public List<Snack> searchSnackById(Integer categoryId, String content) {
        try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            SearchMapper mapper = sqlSession.getMapper(SearchMapper.class);
            List<Snack> list = null;
            try {
                list = mapper.search(categoryId, content);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list != null ? list : Collections.emptyList();
        }
    }

	@Override
	public List<Snack> searchSnack(String content) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            SearchMapper mapper = sqlSession.getMapper(SearchMapper.class);
            List<Snack> list = null;
            try {
                list = mapper.searchByContent(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list != null ? list : Collections.emptyList();
        }
	}

	@Override
	public List<Order> searchOrderByContent(String content) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
            SearchMapper mapper = sqlSession.getMapper(SearchMapper.class);
            List<Order> list = null;
            try {
                list = mapper.searchOrderByContent(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list != null ? list : Collections.emptyList();
        }
	}
}
